package com.longge.wechat.controller.web;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.longge.wechat.message.entity.WxUserEntity;
import com.longge.wechat.message.entity.WebAccessToken;
import com.longge.wechat.message.wxUtil.WeiXinUtil;
import com.longge.wechat.utils.ReadProperties;

@Controller
public class WebRootController {
	
	@RequestMapping(value = "/getUserInfo.do", method = RequestMethod.POST)
	public ModelAndView getUserInfo(HttpServletRequest request, HttpServletResponse response) {
		String code = request.getParameter("code");
		String appid = ReadProperties.loadProperties().getProperty("appid");
		String appsecret=ReadProperties.loadProperties().getProperty("appsecret");

		WebAccessToken webAccessToken = WeiXinUtil.getWebAccessToken(appid, appsecret, code);
		webAccessToken = WeiXinUtil.getRefreshAccessToken(appid, webAccessToken.getRefresh_token());
		WxUserEntity userEntity = WeiXinUtil.getUserEntity(webAccessToken.getAccess_token(), webAccessToken.getOpenId());
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("user", userEntity);
		mav.setViewName("userinfo");
		return mav;
	}

}
